package logic

import (
	"context"
	"team.wphr.vip/technology-group/erp/erp-api-go/common/client"
	"team.wphr.vip/technology-group/erp/erp-api-go/common/util"
	"team.wphr.vip/technology-group/erp/erp-api-go/idl/exterr"
	"team.wphr.vip/technology-group/erp/erp-api-go/idl/proto"
	Sdk_Go_Grpc_ErpServiceGo "team.wphr.vip/technology-group/sdk/sdk-go-grpc-erpservicego"
)

var UPPER_GO_API_MODULE_HUMPLogicInstance UPPER_GO_API_MODULE_HUMPLogic

type UPPER_GO_API_MODULE_HUMPLogic struct {
}

func (this UPPER_GO_API_MODULE_HUMPLogic) formatDetail (info *Sdk_Go_Grpc_ErpServiceGo.UPPER_GO_API_MODULE_HUMPDetailDataResp) (data *proto.UPPER_GO_API_MODULE_HUMPDetailDataResp) {
	return &proto.UPPER_GO_API_MODULE_HUMPDetailDataResp{
		FORMAT_DETAIL_DATA_KEY_VEL
	}
}

//GetUPPER_GO_API_MODULE_HUMPDetail GO_API_MODULE_DESC - 详情
func (this UPPER_GO_API_MODULE_HUMPLogic) GetUPPER_GO_API_MODULE_HUMPDetail(ctx context.Context, req *proto.UPPER_GO_API_MODULE_HUMPDetailReq) (data *proto.UPPER_GO_API_MODULE_HUMPDetailDataResp, err error) {
	managerIds, err := util.GetManagerIds(ctx)
	if err != nil {
		err = exterr.WithMessagef(err, "GetUPPER_GO_API_MODULE_HUMPDetail GetManagerIds error")
		return
	}

	resp, err := client.ErpServiceCli.GetUPPER_GO_API_MODULE_HUMPDetail(ctx, &Sdk_Go_Grpc_ErpServiceGo.UPPER_GO_API_MODULE_HUMPDetailReq{
		Id: req.Id,
		ManagerIds: managerIds,
	})
	err = util.GenOuterErr(resp, err)
	if err != nil {
		return
	}
	if resp.Data == nil {
		err = exterr.New(exterr.E_SERVICE_RETURN_ERROR)
		return
	}

	data = this.formatDetail(resp.Data)

	return
}

//GetUPPER_GO_API_MODULE_HUMPList GO_API_MODULE_DESC - 列表
func (this UPPER_GO_API_MODULE_HUMPLogic) GetUPPER_GO_API_MODULE_HUMPList(ctx context.Context, req *proto.UPPER_GO_API_MODULE_HUMPListReq) (data *proto.UPPER_GO_API_MODULE_HUMPListDataResp, err error) {
	resp, err := client.ErpServiceCli.GetUPPER_GO_API_MODULE_HUMPList(ctx, &Sdk_Go_Grpc_ErpServiceGo.UPPER_GO_API_MODULE_HUMPListReq{
		Offset:          req.GetOffset(),
		Limit:           req.GetLimit(),
	})

	err = util.GenOuterErr(resp, err)
	if err != nil {
		return
	}
	if resp.Data == nil {
		err = exterr.New(exterr.E_SERVICE_RETURN_ERROR)
		return
	}

	data = &proto.UPPER_GO_API_MODULE_HUMPListDataResp{}
	data.Total = resp.Data.Total
	data.MaxPage = resp.Data.MaxPage

	for _, v := range resp.Data.List {
		data.List = append(data.List, this.formatDetail(v))
	}

	return
}

//AddUPPER_GO_API_MODULE_HUMP GO_API_MODULE_DESC - 添加
func (this UPPER_GO_API_MODULE_HUMPLogic) AddUPPER_GO_API_MODULE_HUMP(ctx context.Context, req *proto.AddUPPER_GO_API_MODULE_HUMPReq) (data *proto.AddUPPER_GO_API_MODULE_HUMPDataResp, err error) {
	tmpReq := &Sdk_Go_Grpc_ErpServiceGo.AddUPPER_GO_API_MODULE_HUMPReq{
		ADD_DATA_KEY_VAL
	}

	resp, err := client.ErpServiceCli.AddUPPER_GO_API_MODULE_HUMP(ctx, tmpReq)

	err = util.GenOuterErr(resp, err)
	if err != nil {
		return
	}
	if resp.Data == nil {
		err = exterr.New(exterr.E_SERVICE_RETURN_ERROR)
		return
	}

	data = &proto.AddUPPER_GO_API_MODULE_HUMPDataResp{
		Id: resp.Data.Id,
	}

	return
}

//UpdateUPPER_GO_API_MODULE_HUMP GO_API_MODULE_DESC - 更新
func (this UPPER_GO_API_MODULE_HUMPLogic) UpdateUPPER_GO_API_MODULE_HUMP(ctx context.Context, req *proto.UpdateUPPER_GO_API_MODULE_HUMPReq) (data *proto.UpdateUPPER_GO_API_MODULE_HUMPDataResp, err error) {
	tmpReq := &Sdk_Go_Grpc_ErpServiceGo.UpdateUPPER_GO_API_MODULE_HUMPReq{
		UPDATE_DATA_KEY_VAL
	}

	resp, err := client.ErpServiceCli.UpdateUPPER_GO_API_MODULE_HUMP(ctx, tmpReq)

	err = util.GenOuterErr(resp, err)
	if err != nil {
		return
	}
	if resp.Data == nil {
		err = exterr.New(exterr.E_SERVICE_RETURN_ERROR)
		return
	}

	data = &proto.UpdateUPPER_GO_API_MODULE_HUMPDataResp{
		Id: resp.Data.Id,
	}

	return
}

//DeleteUPPER_GO_API_MODULE_HUMP GO_API_MODULE_DESC - 删除
func (this UPPER_GO_API_MODULE_HUMPLogic) DeleteUPPER_GO_API_MODULE_HUMP(ctx context.Context, req *proto.DeleteUPPER_GO_API_MODULE_HUMPReq) (data *proto.DeleteUPPER_GO_API_MODULE_HUMPDataResp, err error) {
	managerIds, err := util.GetManagerIds(ctx)
	if err != nil {
		err = exterr.WithMessagef(err, "DeleteUPPER_GO_API_MODULE_HUMP GetManagerIds error")
		return
	}
	tmpReq := &Sdk_Go_Grpc_ErpServiceGo.DeleteUPPER_GO_API_MODULE_HUMPReq{
		Id: req.Id,
		ManagerIds: managerIds,
	}

	resp, err := client.ErpServiceCli.DeleteUPPER_GO_API_MODULE_HUMP(ctx, tmpReq)

	err = util.GenOuterErr(resp, err)
	if err != nil {
		return
	}
	if resp.Data == nil {
		err = exterr.New(exterr.E_SERVICE_RETURN_ERROR)
		return
	}

	data = &proto.DeleteUPPER_GO_API_MODULE_HUMPDataResp{
		Id: resp.Data.Id,
	}

	return
}
